//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.imagemanager.util;

import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;

public abstract class BaseCancelable<T> implements Cancelable<T> {
    private static final int STATE_INITIAL = 1;
    private static final int STATE_EXECUTING = 2;
    private static final int STATE_CANCELING = 4;
    private static final int STATE_CANCELED = 8;
    private static final int STATE_ERROR = 16;
    private static final int STATE_COMPLETE = 32;
    private int mState = 1;
    private Throwable mError;
    private T mResult;
    private Cancelable<?> mCurrentTask;

    public BaseCancelable() {
    }

    protected abstract T execute() throws Exception;

    protected void freeCanceledResult(T result) {
    }

    private boolean isInStates(int states) {
        return (states & this.mState) != 0;
    }

    private T handleTerminalStates() throws ExecutionException {
        if(this.mState == 8) {
            throw new CancellationException();
        } else if(this.mState == 16) {
            throw new ExecutionException(this.mError);
        } else if(this.mState == 32) {
            return this.mResult;
        } else {
            throw new IllegalStateException();
        }
    }

    public synchronized void await() throws InterruptedException {
        while(!this.isInStates(56)) {
            this.wait();
        }

    }

    public final T get() throws InterruptedException, ExecutionException {
        synchronized(this) {
            if(this.mState != 1) {
                this.await();
                return this.handleTerminalStates();
            }

            this.mState = 2;
        }

        try {
            this.mResult = this.execute();
        } catch (CancellationException var5) {
            this.mState = 8;
        } catch (InterruptedException var6) {
            this.mState = 8;
        } catch (Throwable var7) {
            Throwable error = var7;
            synchronized(this) {
                if(this.mState != 4) {
                    this.mError = error;
                    this.mState = 16;
                }
            }
        }

        synchronized(this) {
            if(this.mState == 4) {
                this.mState = 8;
            }

            if(this.mState == 2) {
                this.mState = 32;
            }

            this.notifyAll();
            if(this.mState == 8 && this.mResult != null) {
                this.freeCanceledResult(this.mResult);
            }

            return this.handleTerminalStates();
        }
    }

    public synchronized boolean requestCancel() {
        if(this.mState == 1) {
            this.mState = 8;
            this.notifyAll();
            return false;
        } else if(this.mState == 2) {
            if(this.mCurrentTask != null) {
                this.mCurrentTask.requestCancel();
            }

            this.mState = 4;
            return true;
        } else {
            return false;
        }
    }
}
